﻿/***********************************************************************
 * <copyright file="WindowLayoutExtension.cs" company="BUCA JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   LinhMC
 * Email:    linhmc@buca.vn
 * Website:
 * Create Date: Friday, November 15, 2013
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.Globalization;
using System.Web.Mvc;

namespace EasyUIMvc.Window
{
    public class WindowLayoutExtension : ExtensionBase
    {
        private readonly WindowSettings _settings;
        private MvcHtmlString _content;

        public WindowLayoutExtension(WindowSettings settings)
        {
            _settings = settings;
        }

        public WindowLayoutExtension Content(string content)
        {
            _content = MvcHtmlString.Create(content);
            return this;
        }
        public override string GetScripts()
        {
            throw new NotImplementedException();
        }
        public override MvcHtmlString GetHtml()
        {
            var window = GetTopTag("div", "easyui-window", _settings);
            var layoutTag = GetInnerTag("div", "easyui-layout", "", "fit:true", "");
            var region = GetInnerTag("div", "", "", "region:'center'", "padding:10px;");
            region.InnerHtml += _content;
            layoutTag.InnerHtml += region;

            var buttonDataOption = "region:'south', border:" + _settings.DataOptionButton.Border.ToString().ToLower();
            var buttonStyle = "text-align:" + _settings.DataOptionButton.Align.ToLower() + ";padding:" + _settings.DataOptionButton.Padding +";";
            var button = GetInnerTag("div", "", "", buttonDataOption, buttonStyle);

            for (int i = 0; i < _settings.DataOptionButton.ButtonOption.ButtonTexts.Length; i++)
            {
                if (!string.IsNullOrEmpty(_settings.DataOptionButton.ButtonOption.ButtonTexts[i]))
                {
                    var tag = new TagBuilder("a");
                    tag.AddCssClass("easyui-linkbutton");
                    if (!string.IsNullOrEmpty(_settings.DataOptionButton.ButtonOption.ButtonIcons[i].ToString(CultureInfo.InvariantCulture)))
                    {
                        tag.Attributes.Add("data-options", _settings.DataOptionButton.ButtonOption.ButtonIcons[i].ToString(CultureInfo.InvariantCulture));
                    }
                    if (!string.IsNullOrEmpty(_settings.DataOptionButton.ButtonOption.Hrefs[i]))
                    {
                        tag.Attributes.Add("href", _settings.DataOptionButton.ButtonOption.Hrefs[i]);
                    }
                    if (!string.IsNullOrEmpty(_settings.DataOptionButton.ButtonOption.OnClicks[i]))
                    {
                        tag.Attributes.Add("onclick", _settings.DataOptionButton.ButtonOption.OnClicks[i]);
                    }
                    tag.InnerHtml += _settings.DataOptionButton.ButtonOption.ButtonTexts[i];
                    button.InnerHtml += tag;
                }
            }
            layoutTag.InnerHtml += button;
            window.InnerHtml += layoutTag;

            window.MergeAttribute("style", _settings.Style);
            window.MergeAttribute("title", _settings.Title);
            return MvcHtmlString.Create(window.ToString());
        }
    }

    public class WindowSettings : SettingsBase
    {
        public string Title { get; set; }
        public string Style { get; set; }
        public ButtonDataOption DataOptionButton { get; set; }
    }

    public class WindowButton
    {
        public string[] ButtonTexts { get; set; }
        public string[] ButtonIcons { get; set; }
        public string[] Hrefs { get; set; }
        public string[] OnClicks { get; set; }
    }

    public class ButtonDataOption
    {
        public bool Border { get; set; }
        public string Align { get; set; }
        public string Padding { get; set; }
        public WindowButton ButtonOption { get; set; }
    }
}
